package hhh.app.presenter.ui.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.view.ViewBaseAction;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import hhh.app.R;
import hhh.app.data.bean.Category;
import hhh.app.presenter.ui.adapter.CategoryAdapter;

/**
 * Created by Administrator on 2017/1/3 0003.
 */
public class CategoryView extends LinearLayout implements ViewBaseAction {
    private ListView listView1;
    private ListView listView2;

    private CategoryAdapter adapter1;
    private CategoryAdapter adapter2;

    private List<Category> group=new ArrayList<Category>();
//    private SparseArray<List<Category>> children=new SparseArray<List<Category>>();
    private List<Category> childrenItem=new ArrayList<Category>();
    private Map<Integer,List<Category>> keyCategory;

    private int groupSelected=0;
    private String showString="不限";

    public void setKeyCategory(Map<Integer, List<Category>> keyCategory) {
        this.keyCategory = keyCategory;
//        group=this.keyCategory.get(0);
        Category ca=new Category();
        ca.setName("不限");
        ca.setCategory_id(-1);
        group.clear();
        group.add(ca);
        group.addAll(this.keyCategory.get(0));
        adapter1.notifyDataSetChanged();
//        childrenItem=this.keyCategory.toArray()[0];
        childrenItem.clear();
//        childrenItem.addAll(this.keyCategory.get(group.get(0).getCategory_id()));
        adapter2.notifyDataSetChanged();
    }

    public void setOnSelectListener(OnSelectListener onSelectListener) {
        this.onSelectListener = onSelectListener;
    }

    private OnSelectListener onSelectListener;

    public CategoryView(Context context) {
        super(context);
        init(context);
    }

    public CategoryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CategoryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_category,this,true);
        listView1= (ListView) findViewById(R.id.listview1);
        listView2= (ListView) findViewById(R.id.listview2);

        adapter1=new CategoryAdapter(context,R.layout.choose_item,group);
        adapter1.setOnItemClickListener(new CategoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int pos) {
                if(pos<group.size()) {
                    childrenItem.clear();
                    List<Category> categoryList=keyCategory.get(group.get(pos).getCategory_id());
                    if(categoryList!=null) {
                        childrenItem.addAll(categoryList);
                        adapter2.notifyDataSetChanged();
                    }else{
                        showString=group.get(pos).getName();
                        if(onSelectListener!=null){
                            onSelectListener.onSelectListener(group.get(pos).getCategory_id());
                        }
                    }
                }
            }
        });

        adapter2=new CategoryAdapter(context,R.layout.choose_item,childrenItem);
        adapter2.setOnItemClickListener(new CategoryAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(View v, int pos) {
                Category category=childrenItem.get(pos);
                if(category!=null){
                    showString=category.getName();
                    if(onSelectListener!=null){
                        onSelectListener.onSelectListener(category.getCategory_id());
                    }
                }

            }
        });

        listView1.setAdapter(adapter1);
        listView2.setAdapter(adapter2);
    }

    public String getShowText(){
        return showString;
    }

    public interface OnSelectListener{
        public void onSelectListener(int val);
    }

    @Override
    public void hide() {

    }

    @Override
    public void show() {

    }
}
